package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.LunarUtil;

import java.util.ArrayList;
import java.util.List;

public class Yun {
    private int gender;
    private int startYear;
    private int startMonth;
    private int startDay;
    private int startHour;
    private boolean forward;
    private Lunar lunar;

    public Yun(EightChar eightChar,int gender,int sect) {
        this.lunar = eightChar.getLunar();
        this.gender = gender;
        boolean yang = 0 == this.lunar.getYearGanIndexExact() % 2;
        boolean man = 1 == gender;
        this.forward = (yang && man) || (!yang && !man);
        this.computeStart(sect);
    }


    private void computeStart(int sect)
    {
        JieQi prev = this.lunar.getPrevJie();
        JieQi next = this.lunar.getNextJie();
        Solar current = this.lunar.getSolar();
        Solar start = this.forward ? current : prev.getSolar();
        Solar end = this.forward ? next.getSolar() : current;
        int hour = 0,year,month,day;
        if (2 == sect) {
            int minutes = end.subtractMinute(start);
            year = (int)(minutes / 4320);
            minutes -= year * 4320;
            month = (int)(minutes / 360);
            minutes -= month * 360;
            day = (int)(minutes / 12);
            minutes -= day * 12;
            hour = minutes * 2;
        } else {
            int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring( 11, 16));
            int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
            int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
            int dayDiff = end.subtract(start);
            if (hourDiff < 0) {
                hourDiff += 12;
                dayDiff--;
            }
            int monthDiff = (int)(hourDiff * 10 / 30);
            month = dayDiff * 4 + monthDiff;
            day = hourDiff * 10 - monthDiff * 30;
            year = (int)(month / 12);
            month = month - year * 12;
        }
        this.startYear = year;
        this.startMonth = month;
        this.startDay = day;
        this.startHour = hour;
    }

    public int getGender()
    {
        return this.gender;
    }

    public int getStartYear()
    {
        return this.startYear;
    }

    public int getStartMonth()
    {
        return this.startMonth;
    }

    public int getStartDay()
    {
        return this.startDay;
    }

    public int getStartHour()
    {
        return this.startHour;
    }

    public boolean isForward()
    {
        return this.forward;
    }

    public Lunar getLunar()
    {
        return this.lunar;
    }

    public Solar getStartSolar()
    {
        Solar solar = this.lunar.getSolar();
        solar = solar.nextYear(this.startYear);
        solar = solar.nextMonth(this.startMonth);
        solar = solar.next(this.startDay);
        return solar.nextHour(this.startHour);
    }

    public List<DaYun> getDaYun()
    {
        return this.getDaYunBy(10);
    }

    public List<DaYun> getDaYunBy(int n)
    {
        List<DaYun> l = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            l.add(new DaYun(this, i));
        }
        return l;
    }

    public static void main(String[] args) {
        System.out.println("Yun");
    }
}
